<template>
	<view v-if="list">
		<view class="u-tabbar safe-area-inset-bottom">
			<view class="u-tabbar-item" v-for="(item, index) in list" :key="index"
				:class="{ bulge: isBulge && index == 2 }" @click="redirectTo(item.link)">
				<view class="tabbar-icon u-flex u-flex u-flex-nowrap u-row-between">
					<image class="icon" :lazy-load="true"
						:src="verify(item.link) ? $util.img(item.selectedIconPath) : $util.img(item.iconPath)" />
					<view class="cartNumberBtn font-size-activity-tag color-base-bg"
						v-if="item.link.wap_url == '/goods/cart/cart' && cartNumber > 0">
						{{ cartNumber }}
					</view>
					<view class="u-tabbar-label">{{item.text}}</view>
				</view>
			</view>
		</view>
		<view class="u-fixed-placeholder safe-area-inset-bottom"></view>
	</view>
</template>

<script>
	export default {
		name: 'diy-bottom-nav',
		props: {
			value: {
				type: Object
			},
			type: {
				type: String,
				default: ''
			}
		},

		data() {
			return {
				currentRoute: '', //当前页面路径
				//特殊页面处理悬浮
				special: ['pages/goods/cart/cart'],
				specialPage: '',
				isIphoneX: false, //判断手机是否是iphoneX以上
				jumpFlag: true, //是否可以跳转，防止重复点击
				bottomNav: {},
				textHoverColor: '',
				textColor: '',
				backgroundColor: '',
				list: {},
				bottomType: 1
			};
		},
		mounted() {
			let currentPage = getCurrentPages()[getCurrentPages().length - 1];
			this.currentRoute = currentPage.route;
			this.getBottomNav();
			this.$store.dispatch('getCartNumber');
		},
		computed: {
			cartNumber() {
				return this.$store.state.cartNumber;
			},
			isBulge() {
				// 暂时去掉悬浮功能
				return false;
				if (this.bottomNav.list) {
					return this.bottomNav.list.length == 5 && this.special.indexOf(this.currentRoute) == -1 && this
						.bottomNav.bulge;
				} else {
					return false;
				}
			}
		},
		methods: {
			redirectTo(link) {
				this.$emit('callback');
				if (!this.jumpFlag) return;
				this.jumpFlag = false;
				setTimeout(() => {
					this.jumpFlag = true;
				}, 300);
				if (link == null || link == '' || !link.wap_url) return false;
				if (link.wap_url.indexOf(this.currentRoute) != -1) return false;

				let jump = true;
				let arr = getCurrentPages().reverse();
				for (let i = 0; i < arr.length; i++) {
					if (link.wap_url.indexOf(arr[i].route) != -1) {
						jump = false;
						uni.navigateBack({
							delta: i
						});
						break;
					}
				}
				if (jump) {
					this.$util.redirectTo(link.wap_url);
				}
			},
			verify(link) {
				if (link == null || link == '' || !link.wap_url) return false;
				if (link.wap_url.indexOf(this.currentRoute) != -1) {
					return true;
				}
				return false;
			},
			getBottomNav() {
				let url = '/api/diyview/bottomNav';
				let data = {};
				if (this.siteId) {
					url = '/api/diyview/shopBottomNav';
					data.site_id = this.siteId;
				}

				this.$api.sendRequest({
					url: url,
					data: data,
					success: res => {
						let data = res.data;
						if (data && data.value && data.value.length) {
							var value = JSON.parse(data.value);
							this.bottomNav = value;
							this.textColor = value.textColor;
							this.textHoverColor = value.textHoverColor;
							this.backgroundColor = value.backgroundColor;
							this.bottomType = value.type;
							this.list = value.list;
							this.$store.commit('setTabbarList', JSON.parse(data.value));
							this.$forceUpdate();
						}
					}
				});
			}
		}
	};
</script>

<style lang="scss">
	@mixin vue-flex($direction: row) {
		/* #ifndef APP-NVUE */
		display: flex;
		flex-direction: $direction;
		/* #endif */
	}

	.u-fixed-placeholder {
		/* #ifndef APP-NVUE */
		box-sizing: content-box;
		/* #endif */
	}

	.u-tabbar {
		@include vue-flex;
		align-items: center;
		position: relative;
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		z-index: 998;
		/* #ifndef APP-NVUE */
		box-sizing: content-box;
		/* #endif */
		background: #FFFFFF;
		height: 50px;
	}

	.u-tabbar-item {
		flex: 1;
		justify-content: center;
		height: 100%;
		padding: 12rpx 0;
		@include vue-flex;
		flex-direction: column;
		align-items: center;
		position: relative;
	}

	.u-tabbar-label {
		color: $u-content-color;
		font-size: 26rpx;
		line-height: 28rpx;
		position: absolute;
		bottom: 14rpx;
		left: 50%;
		transform: translateX(-50%);
		width: 100%;
		text-align: center;
	}

	.icon {
		width: 40rpx;
		height: 40rpx;
		position: relative;
		display: inline-block;
		margin-top: 10rpx;
		margin-bottom: 40rpx;
	}

	.cartNumberBtn {
		position: absolute;
		top: -8rpx;
		right: -18rpx;
		width: 24rpx;
		height: 24rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #fff;
		padding: 6rpx;
		border-radius: 50%;
		z-index: 99;
	}
</style>
